package com.blog.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

	/**
	 * 格式化当前时间
	 * 
	 * @param farmat
	 * @return
	 */
	public static String formatData(String farmat) {
		SimpleDateFormat sdf = new SimpleDateFormat(farmat);
		return sdf.format(new Date());
	}

	/**
	 * 格式化时间
	 * 
	 * @param farmat
	 * @param date
	 * @return
	 */
	public static String formatData(String farmat, Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(farmat);
		return sdf.format(date);
	}

	public static String convertFormat(String farmatBefore, String farmatAfter,
			String date) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(farmatBefore);
			Date d = sdf.parse(date);
			SimpleDateFormat sdfAfter = new SimpleDateFormat(farmatAfter);
			return sdfAfter.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static long getTime(String date, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date d = sdf.parse(date);
			return d.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public final static long TIME_SECOND = 1000;
	public final static long TIME_MINUTE = 60 * TIME_SECOND;
	public final static long TIME_HOUR = TIME_MINUTE * 60;
	public final static long TIME_DAY = TIME_HOUR * 24;
	
	/**
	 * 计算原有时间相加一定小时或天数后的时间
	 * 
	 * @param beforeTime原时间
	 * @param timeOutTime相加的小时数或天数
	 * @param type类型
	 *            1：小时，2：天
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String addDayOrHour(String beforeDate, Double difference,
			int type, String format) {
		if (CommonUtil.isEmpty(beforeDate)) {
			return "";
		}

		if (difference.isNaN()) {
			return beforeDate;
		}

		long beforeTime = 0;

		beforeTime = getTime(beforeDate, format);

		long differenceTime = 0;
		if (type == 2) {// 把天换算成秒
			differenceTime = (long) (difference * TIME_DAY);
		} else {// 把小时换算成秒
			differenceTime = (long) (difference * TIME_HOUR);
		}

		long time = beforeTime + differenceTime;
		Date d = new Date(time);
		return formatData("yyyy-MM-dd HH:mm:ss", d);
	}
	
	/**
	 * 计算原有时间相加一定小时或天数后的时间
	 * 
	 * @param beforeTime原时间
	 * @param timeOutTime相加的小时数或天数
	 * @param type类型
	 *            1：小时，2：天
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static long addDayOrHourToLong(String beforeDate, Double difference,
			int type, String format) {
		if (CommonUtil.isEmpty(beforeDate)) {
			return 0;
		}

		if (difference.isNaN()) {
			return 0;
		}

		long beforeTime = 0;

		beforeTime = getTime(beforeDate, format);

		long differenceTime = 0;
		if (type == 2) {// 把天换算成秒
			differenceTime = (long) (difference * TIME_DAY);
		} else {// 把小时换算成秒
			differenceTime = (long) (difference * TIME_HOUR);
		}

		long time = beforeTime + differenceTime;
		return time;
	}

	/**
	 * 获取时长X天X小时X分X秒
	 * 
	 * @param time
	 * @return
	 */
	public static String getTimeLength(long time) {
		String str = "";
		long day = time / TIME_DAY;
		long hour = (time % TIME_DAY) / TIME_HOUR;
		long minute = (time % TIME_HOUR) / TIME_MINUTE;
		long second = (time % TIME_MINUTE) / TIME_SECOND;
		if (day > 0) {
			str += day + "天" + hour + "时" + minute + "分" + second + "秒";
		} else if (hour > 0) {
			str += hour + "时" + minute + "分" + second + "秒";
		} else if (minute > 0) {
			str += minute + "分" + second + "秒";
		} else if (second > 0) {
			str += second + "秒";
		}
		return str;

	}

	public static void main(String[] args) {
		System.out.println(DateUtil.convertFormat("yyyy-MM-dd","M月dd日","2012-12-13"));
		
		//DateUtil.getTime("20111030121310", "yyyyMMddHHmmss");
		System.out.println("\u626b");
		//System.out.println(getTimeLength(new Date().getTime()-DateUtil.getTime("20111010121310", "yyyyMMddHHmmss")));
	}
}
